<template>
  <div>
    <el-card>
      <div slot="header" class="clearfix">
        <my-breadcrumb>粉丝管理</my-breadcrumb>
      </div>
      <!-- tabs -->
      <el-tabs v-model="activeName" type="card" @tab-click="hTabChange">
        <el-tab-pane label="列表" name="list">
          <div class="fans_list">
            <div class="fans_item" v-for="item in fans" :key="item.id.toString()">
              <el-avatar :size="80" :src="item.photo"></el-avatar>
              <p>{{item.name}}</p>
              <el-button type="primary" plain size="small">+关注</el-button>
            </div>
          </div>

          <!-- 分页 -->
            <!-- @current-change="hPageChange" -->
          <el-pagination
            style="margin-top: 1em;"
            background
            :page-size="per_page"
            layout="prev, pager, next"
            @current-change="hPageChange"
            :total="total_count">
          </el-pagination>

        </el-tab-pane>
        <el-tab-pane label="画像" name="echart">画像
            <div id="main" style="width: 600px;height:400px;"></div>
        </el-tab-pane>
      </el-tabs>
    </el-card>

  </div>
</template>

<script>
import echarts from 'echarts'
export default {
  name: 'my-fans',
  data () {
    return {
      per_page: 25, // 每页显示几条
      page: 1, // 当前页码
      total_count: 0, // 总数据条数
      activeName: 'list',
      fans: []
    }
  },
  created () {
    this.loadFans()
  },
  methods: {
    // 发送请求获取数据
    async loadFans () {
      try {
        const res = await this.$http({
          url: '/mp/v1_0/followers',
          method: 'GET',
          params: {
            page: this.page,
            per_page: this.per_page
          }
        })
        console.log(res)
        this.fans = res.data.data.results
        this.total_count = res.data.data.total_count
      } catch (err) {
        console.log(err)
      }
    },
    // 分页功能
    hPageChange (page) {
      this.page = page
      this.loadFans()
    },
    // 切换到画图页面
    hTabChange (tab) {
      if (tab.name === 'echart') {
        this.draw()
      }
    },
    buildData (ageObj) {
      const xAxis = Object.keys(ageObj).map(item => {
        return item.replace('gt', '大于').replace('le', '小于')
      })
      const data = Object.values(ageObj)
      return {
        xAxis,
        data
      }
    },
    async draw () {
      const myChart = echarts.init(document.getElementById('main'))
      // 发送请求拿取数据
      const res = await this.$http({
        url: '/mp/v1_0/statistics/followers',
        method: 'GET'
      })
      console.log(res)
      const result = this.buildData(res.data.data.age)

      // 指定图表的配置项和数据
      const option = {
        title: {
          text: 'ECharts 入门示例'
        },
        tooltip: {},
        legend: {
          data: ['销量']
        },
        xAxis: {
          data: result.xAxis
        },
        yAxis: {},
        series: [
          { name: '销量', type: 'line', data: result.data }]
      }

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option)
    }
  }
}
</script>

<style scoped lang='less'>
.fans_list {
  .fans_item {
    width: 120px;
    height: 170px;
    border: 1px dashed #ddd;
    text-align: center;
    padding-top: 10px;
    display: inline-block;
    margin-right: 30px;
    margin-bottom: 30px;
    p {
      margin: 10px 0;
    }
  }
}
</style>
